﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace SmartMeterParamFrame.Model.DIValueConverters
{
    /// <summary>
    /// DI 数据转换类
    /// 根据DI 与 数据进行相互的转换
    /// 如果内部没有定义相关的转换，则返回转入的值 
    /// </summary>
    public class DIValueConverter
    {
        /// <summary>
        /// 0到9的字符集合
        /// </summary>
        public static char[] NUMBER_ALL = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        /// <summary>
        /// 1到9的数据集合
        /// </summary>
        public static char[] NUMBER_POSITIVE = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        private static char[] NUMBER_HEX = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

        /// <summary>
        /// 所有的转换器
        /// </summary>
        static List<IDIDataConverter> converters = new List<IDIDataConverter>();

        static DIValueConverter()
        {
            //将所有的转换器添加到这里
            converters.Add(new Converter_FeiLvDiaJia());
            converters.Add(new Converter_Date());
            converters.Add(new Converter_DateTime());
            converters.Add(new Converter_MeterNumber());
            converters.Add(new Converter_ShortTime());
            converters.Add(new Converter_MoneyAndEnergy());
            converters.Add(new Converter_HuGan());
            converters.Add(new Converter_2BCD());
            converters.Add(new Converter_JeiShuanRi());
            converters.Add(new Converter_JeiTiZhi());
            converters.Add(new Converter_ShiQuAndRiShiDuan());
            converters.Add(new Converter_JiaRi());
        }

        /// <summary>
        /// 根据 要转换的Di 获取一个转换器
        /// </summary>
        /// <param name="di"></param>
        /// <returns></returns>
        static IDIDataConverter GetConverter(string di)
        {
            foreach (IDIDataConverter parser in converters)
            {
                if (parser.CanConverter(di))
                    return parser;
            }
            return null;
        }

        /// <summary>
        /// 检测字符中，是否所有的字符都为 0--F
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsAllHEX(string s)
        {
            if (s == null || s.Length == 0)
                return false;
            foreach (char c in s)
            {
                if (NUMBER_HEX.Contains(char.ToUpper(c)))
                    continue;
                else
                {
                    return false;
                }
            }
            return true;
        }


        /// <summary>
        /// 将一个指定di底层数据形式值，转换成可以阅读的值
        /// </summary>
        /// <param name="di"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ConvertToUI(string di, string value)
        {
            IDIDataConverter converter = GetConverter(di);

            if (converter == null)
                return value;

            string newValue = converter.ConvertToUI(di, value);
            return newValue;
        }


        /// <summary>
        /// 将可以阅读的值，转换成底层的数据形式
        /// </summary>
        /// <param name="di"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ConvertToLow(string di, string value)
        {
            IDIDataConverter converter = GetConverter(di);

            if (converter == null)
                return value;

            string newValue = converter.ConvertToLow(di, value);
            return newValue;
        }

        /// <summary>
        /// 校验一个值，是否合法
        /// </summary>
        /// <param name="di"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ValidateValue(string di, string value)
        {
            IDIDataConverter converter = GetConverter(di);

            if (converter == null)
                return true;
            bool isOK = converter.ValidateValue(di, value);
            return isOK;
        }
    }
}
